package com.b.service.impl;

import com.b.repository.AdminRepository;
import com.b.utils.MD5Util;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import com.b.dao.AdminMapper;
import com.b.domain.Admin;
import com.b.service.AdminService;

import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService{

    @Resource
    private AdminMapper adminMapper;
    @Resource
    private AdminRepository adminRepository;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Admin record) {
        return adminMapper.insert(record);
    }

    @Override
    public int insertSelective(Admin record) {
        record.setPasswordHash(MD5Util.code(record.getPasswordHash()));
        record.setCreateTime(new Date());
        System.out.println( record);
        return adminMapper.insertSelective(record);
    }

    @Override
    public Admin selectByPrimaryKey(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Admin record) {
        return adminMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Admin record) {
        return adminMapper.updateByPrimaryKey(record);
    }

    @Override
    public Admin findByUsername(String username) {
        Admin admin = null;
        if (username != null) {
            admin = adminRepository.findByUsername(username);
        }
        if (admin == null) {
            admin = adminMapper.findByUsername(username);
        }
        return admin;
    }


    @Override
    public List<Admin> getAll() {
        return adminMapper.getAll();
    }

}
